home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / gimp-2.6.5-i686-setup.exe / {app} / lib / gimp / 2.0 / python / gimpui,1.py < prev    next >
Encoding:
Python Source  |  2009-02-15  |  7.7 KB  |  229 lines

  1. #   Gimp-Python - allows the writing of Gimp plugins in Python.
  2. #   Copyright (C) 1997  James Henstridge <james@daa.com.au>
  3. #
  4. #    This program is free software; you can redistribute it and/or modify
  5. #   it under the terms of the GNU General Public License as published by
  6. #   the Free Software Foundation; either version 2 of the License, or
  7. #   (at your option) any later version.
  8. #
  9. #   This program is distributed in the hope that it will be useful,
  10. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #   GNU General Public License for more details.
  13. #
  14. #   You should have received a copy of the GNU General Public License
  15. #   along with this program; if not, write to the Free Software
  16. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  
  18. '''This module implements the UI items found in the libgimpui library.
  19. It requires pygtk to work.  These functions take use to callbacks -- one
  20. is a constraint function, and the other is the callback object.  The
  21. constraint function takes an image object as its first argument, and
  22. a drawable object as its second if appropriate.  The callback functions
  23. get the selected object as their first argument, and the user data as
  24. the second.
  25.  
  26. It also implements a number of selector widgets, which can be used to select
  27. various gimp data types.  Each of these selectors takes default as an argument
  28. to the constructor, and has a get_value() method for retrieving the result.
  29. '''
  30.  
  31. import pygtk
  32. pygtk.require('2.0')
  33.  
  34. import gtk, gobject, gimp, gimpcolor
  35.  
  36. from _gimpui import *
  37.  
  38. import gettext
  39. t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True)
  40. _ = t.ugettext
  41.  
  42. def _callbackWrapper(menu_item, callback, data):
  43.     callback(menu_item.get_data("Gimp-ID"), data)
  44.  
  45. def _createMenu(items, callback, data):
  46.     menu = gtk.Menu()
  47.     if not items:
  48.         items = [("(none)", None)]
  49.     for label, id in items:
  50.         menu_item = gtk.MenuItem(label)
  51.         menu_item.set_data("Gimp-ID", id)
  52.         menu.add(menu_item)
  53.         if callback:
  54.             menu_item.connect("activate", _callbackWrapper,
  55.                               callback, data)
  56.         menu_item.show()
  57.     return menu
  58.  
  59.  
  60. def ImageMenu(constraint=None, callback=None, data=None):
  61.     items = []
  62.     for img in gimp.image_list():
  63.         if constraint and not constraint(img):
  64.             continue
  65.         if not img.filename:
  66.             filename = img.name
  67.         else:
  68.             filename = img.filename
  69.         items.append((filename, img))
  70.     items.sort()
  71.     return _createMenu(items, callback, data)
  72.  
  73. def LayerMenu(constraint=None, callback=None, data=None):
  74.     items = []
  75.     for img in gimp.image_list():
  76.         filename = img.filename
  77.         if not filename:
  78.             filename = img.name
  79.         for layer in img.layers:
  80.             if constraint and not constraint(img, layer):
  81.                 continue
  82.             name = filename + "/" + layer.name
  83.             items.append((name, layer))
  84.     items.sort()
  85.     return _createMenu(items, callback, data)
  86.  
  87. def ChannelMenu(constraint=None, callback=None, data=None):
  88.     items = []
  89.     for img in gimp.image_list():
  90.         filename = img.filename
  91.         if not filename:
  92.             filename = img.name
  93.         for channel in img.channels:
  94.             if constraint and not constraint(img, channel):
  95.                 continue
  96.             name = filename + "/" + channel.name
  97.             items.append((name, channel))
  98.     items.sort()
  99.     return _createMenu(items, callback, data)
  100.  
  101. def DrawableMenu(constraint=None, callback=None, data=None):
  102.     items = []
  103.     for img in gimp.image_list():
  104.         filename = img.filename
  105.         if not filename:
  106.             filename = img.name
  107.         for drawable in img.layers + img.channels:
  108.             if constraint and not constraint(img, drawable):
  109.                 continue
  110.             name = filename + "/" + drawable.name
  111.             items.append((name, drawable))
  112.     items.sort()
  113.     return _createMenu(items, callback, data)
  114.  
  115. def VectorsMenu(constraint=None, callback=None, data=None):
  116.     items = []
  117.     for img in gimp.image_list():
  118.         filename = img.filename
  119.         if not filename:
  120.             filename = img.name
  121.         for vectors in img.vectors:
  122.             if constraint and not constraint(img, vectors):
  123.                 continue
  124.             name = filename + "/" + vectors.name
  125.             items.append((name, vectors))
  126.     items.sort()
  127.     return _createMenu(items, callback, data)
  128.  
  129. class ImageSelector(ImageComboBox):
  130.     def __init__(self, default=None):
  131.         ImageComboBox.__init__(self)
  132.         if default is not None:
  133.             self.set_active_image(default)
  134.     def get_value(self):
  135.         return self.get_active_image()
  136.  
  137. class LayerSelector(LayerComboBox):
  138.     def __init__(self, default=None):
  139.         LayerComboBox.__init__(self)
  140.         if default is not None:
  141.             self.set_active_layer(default)
  142.     def get_value(self):
  143.         return self.get_active_layer()
  144.  
  145. class ChannelSelector(ChannelComboBox):
  146.     def __init__(self, default=None):
  147.         ChannelComboBox.__init__(self)
  148.         if default is not None:
  149.             self.set_active_channel(default)
  150.     def get_value(self):
  151.         return self.get_active_channel()
  152.  
  153. class DrawableSelector(DrawableComboBox):
  154.     def __init__(self, default=None):
  155.         DrawableComboBox.__init__(self)
  156.         if default is not None:
  157.             self.set_active_drawable(default)
  158.     def get_value(self):
  159.         return self.get_active_drawable()
  160.  
  161. class VectorsSelector(VectorsComboBox):
  162.     def __init__(self, default=None):
  163.         VectorsComboBox.__init__(self)
  164.         if default is not None:
  165.             self.set_active_vectors(default)
  166.     def get_value(self):
  167.         return self.get_active_vectors()
  168.  
  169. class ColorSelector(ColorButton):
  170.     def __init__(self, default=gimpcolor.RGB(1.0, 0, 0)):
  171.         if isinstance(default, gimpcolor.RGB):
  172.             color = default
  173.         elif isinstance(default, tuple):
  174.             color = apply(gimpcolor.RGB, default)
  175.         elif isinstance(default, str):
  176.             color = gimpcolor.rgb_parse_css(default)
  177.         ColorButton.__init__(self, _("Python-Fu Color Selection"), 100, 20,
  178.                              color, COLOR_AREA_FLAT)
  179.     def get_value(self):
  180.         return self.get_color();
  181.  
  182. class PatternSelector(PatternSelectButton):
  183.     def __init__(self, default=""):
  184.         PatternSelectButton.__init__(self)
  185.         if default:
  186.             self.set_pattern(default)
  187.     def get_value(self):
  188.         return self.get_pattern()
  189.  
  190. class BrushSelector(BrushSelectButton):
  191.     def __init__(self, default=""):
  192.         BrushSelectButton.__init__(self)
  193.         if default:
  194.             self.set_brush(default, -1.0, -1, -1)
  195.     def get_value(self):
  196.         return self.get_brush()[0]
  197.  
  198. class GradientSelector(GradientSelectButton):
  199.     def __init__(self, default=""):
  200.         GradientSelectButton.__init__(self)
  201.         if default:
  202.             self.set_gradient(default)
  203.     def get_value(self):
  204.         return self.get_gradient()
  205.  
  206. class PaletteSelector(PaletteSelectButton):
  207.     def __init__(self, default=""):
  208.         PaletteSelectButton.__init__(self)
  209.         if default:
  210.             self.set_palette(default)
  211.     def get_value(self):
  212.         return self.get_palette()
  213.  
  214. class FontSelector(FontSelectButton):
  215.     def __init__(self, default="Sans"):
  216.         FontSelectButton.__init__(self)
  217.         if default:
  218.             self.set_font(default)
  219.     def get_value(self):
  220.         return self.get_font()
  221.  
  222. class FileSelector(gtk.FileChooserButton):
  223.     def __init__(self, default=""):
  224.         gtk.FileChooserButton.__init__(self, _("Python-Fu File Selection"))
  225.         if default:
  226.             self.set_filename(default)
  227.     def get_value(self):
  228.         return self.get_filename()
  229.